<template>
  <div class="app-container">
    <el-form :model="queryParam" ref="queryForm" :inline="true">
      <el-form-item label="用户名：">
        <el-input v-model="queryParam.userName"></el-input>
      </el-form-item>
      <el-form-item label="消防队：">
        <el-select v-model="queryParam.level" placeholder="消防队" @change="levelChange" clearable>
          <el-option v-for="item in levelEnum" :key="item.key" :value="item.key" :label="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button  type="danger" @click="downExcel" icon="el-icon-download">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="listLoading" :data="tableData" border fit highlight-current-row style="width: 100%">
      <el-table-column prop="id" width="60px;" label="Id" />
      <el-table-column prop="userName" label="用户名"/>
      <el-table-column prop="realName" label="真实姓名" />
      <el-table-column prop="userLevel" label="消防队"  :formatter="levelFormatter"/>
      <el-table-column prop="sex" label="性别" width="60px;" :formatter="sexFormatter"/>
      <el-table-column prop="phone" label="手机号"/>
      <el-table-column prop="dayNum" label="登录天数"/>
      <el-table-column prop="answerNum" label="做题数量"/>
      <el-table-column prop="totalScore" label="总得分"/>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParam.pageIndex" :limit.sync="queryParam.pageSize"
                @pagination="search"/>

    </div>
</template>
<script>
import XLSX from 'xlsx'
import { mapGetters, mapState } from 'vuex'
import Pagination from '@/components/Pagination'
import userApi from '@/api/user'

export default {
  components: { Pagination },
  data () {
    return {
      queryParam: {
        userName: '',
        role: 1,
        pageIndex: 1,
        pageSize: 10,
        level: null
      },
      listLoading: true,
      tableData: [],
      total: 0
    }
  },
  created () {
    this.search()
  },
  methods: {
    search () {
      this.listLoading = true
      this.queryParam.pageSize = 999
      userApi.getUserPageLists(this.queryParam).then(data => {
        const re = data.response
        this.tableData = re.list
        this.total = re.total
        this.queryParam.pageIndex = re.pageNum
        this.listLoading = false
      })
    },
    downExcel () {
      for (let i = 0; i < this.tableData.length; i++) {
        this.formatIssueKey(this.tableData[i])
      }
      let excelDatas = {
        // 定义表名
        SheetNames: ['统计详情'],
        Sheets: {
          '统计详情': {
            '!merges': []
          }
        }
      }
      // 通过工具将对象转换为表对象
      excelDatas.Sheets['统计详情'] = XLSX.utils.json_to_sheet(this.tableData)
      // 将数据导出到文件中
      XLSX.writeFile(excelDatas, '答题统计.xlsx')
    },
    formatIssueKey (data) {
      for (let key in data) {
        switch (key) {
          case 'userName':
            data['账号'] = data[key]
            delete data[key]
            break
          case 'realName':
            data['姓名'] = data[key]
            delete data[key]
            break
          case 'age':
            data['年龄'] = data[key]
            delete data[key]
            break
          case 'sex':
            data['性别'] = data[key]
            delete data[key]
            break
          case 'phone':
            data['电话'] = data[key]
            delete data[key]
            break
          case 'userLevel':
            data['消防队'] = data[key]
            delete data[key]
            break
          case 'dayNum':
            data['登录天数'] = data[key]
            delete data[key]
            break
          case 'answerNum':
            data['答题数量'] = data[key]
            delete data[key]
            break
          case 'totalScore':
            data['总得分'] = data[key]
            delete data[key]
            break
          default:
            delete data[key]
            break
        }
      }
    },
    changeStatus (row) {
      let _this = this
      userApi.changeStatus(row.id).then(re => {
        if (re.code === 1) {
          row.status = re.response
          _this.$message.success(re.message)
        } else {
          _this.$message.error(re.message)
        }
      })
    },
    deleteUser (row) {
      let _this = this
      userApi.deleteUser(row.id).then(re => {
        if (re.code === 1) {
          _this.search()
          _this.$message.success(re.message)
        } else {
          _this.$message.error(re.message)
        }
      })
    },
    submitForm () {
      this.queryParam.pageIndex = 1
      this.search()
    },
    levelFormatter  (row, column, cellValue, index) {
      return this.enumFormat(this.levelEnum, cellValue)
    },
    levelChange () {
      this.queryParam.subjectId = null
      this.subjectFilter = this.subjects.filter(data => data.level === this.queryParam.level)
    },
    sexFormatter  (row, column, cellValue, index) {
      return this.enumFormat(this.sexEnum, cellValue)
    },
    statusFormatter (status) {
      return this.enumFormat(this.statusEnum, status)
    },
    statusTagFormatter (status) {
      return this.enumFormat(this.statusTag, status)
    },
    statusBtnFormatter (status) {
      return this.enumFormat(this.statusBtn, status)
    }
  },
  computed: {
    ...mapGetters('enumItem', [
      'enumFormat'
    ]),
    ...mapState('enumItem', {
      sexEnum: state => state.user.sexEnum,
      statusEnum: state => state.user.statusEnum,
      statusTag: state => state.user.statusTag,
      statusBtn: state => state.user.statusBtn,
      levelEnum: state => state.user.levelEnum
    })
  }
}
</script>
